from django.http.response import HttpResponse, JsonResponse
from django.shortcuts import render, redirect
from django.urls.base import reverse
from django.views import View

def index(request):

    # response =HttpResponse(content="aaaa中国",content_type="application/json;charset=utf-8", status=202)
    # response["key"] = "AAAAA"
    # response="AAA" django中部支持直接返回任何数据，必须先生成响应体，只能返回响应体对象
    response = JsonResponse([{"paginate":"哈佛的哈佛啊",'city': '北京'},{'city１': '北京','city２': '成都'}],
                            json_dumps_params={"ensure_ascii": False},
                                    safe=False,
                            )
    # response.charset = 'utf-8';
    # response.setdefault('content_type','application/json; charset=utf-8')
    return response

def new1(request):

    # return redirect("users:index")
    return redirect(reverse("users:index"))

def set_cookie(request):
    response = HttpResponse("保存ｃｏｏｋｉｅ信息")
    response.set_cookie("id",1)
    response.set_cookie("name","python")
    return response


def get_cookie(request):

    id= request.COOKIES.get("id")
    name = request.COOKIES.get("name")
    return HttpResponse("cookie: %s, %s"% (name, id))


class PostView(View):

    def get(self,request):
        return render(request, "post2.html")

    def post(self,request):
        title= request.POST.get("title")
        content = request.POST.get("content")


        data={
            "title":title,
            "content":content,
        }
        return render(request, "post.html", data)